package chat.aylan.top.wind;
/**
 * 登录窗口
 */

import chat.aylan.top.commons.Constants;
import chat.aylan.top.commons.LinkLabel;
import chat.aylan.top.commons.User;
import chat.aylan.top.database.entity.UserEntity;
import chat.aylan.top.database.service.UserServicce;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class AuthWind extends JFrame {

        private JButton confirmBtn;
        private JButton cancelBtn;

        private JPanel top;
        private JPanel center;
        private JPanel bottom;

        private JTextField account;
        private JPasswordField passwordField;

        private JLabel error;

        public AuthWind(int width,int height){

                this.setSize(width,height);

                top = new JPanel();
                top.setSize(344,333);
                JLabel title = new JLabel("欢迎使用Oylan聊天室");
                title.setBorder(BorderFactory.createEmptyBorder(20,0,0,0));
                Font font = new Font("宋体", Font.PLAIN, 21);
                title.setFont(font);
                top.add(title);
                center = new JPanel(new BorderLayout(1,2));

                JPanel centerBottom = new JPanel();

                JPanel centerLeft = new JPanel(new GridLayout(4,1));

                Border border= BorderFactory.createEmptyBorder(0,40,0,30);
                JLabel username = new JLabel("用户名 :");
                JLabel password = new JLabel("密码 :");

                username.setBorder(border);
                password.setBorder(border);

                centerLeft.add(new JPanel());
                centerLeft.add(username);
                centerLeft.add(password);

                JPanel centerRight = new JPanel();
                centerRight.setLayout(new GridLayout(4,1));

                account = new JTextField();
                passwordField = new JPasswordField();

                account.setSize(120,35);
                passwordField.setSize(120,30);

                centerRight.add(new JPanel());
                centerRight.add(account);
                centerRight.add(passwordField);
                center.add(centerRight,BorderLayout.CENTER);
                center.add(centerLeft,BorderLayout.WEST);

                JPanel rightEmpty = new JPanel();
                rightEmpty.setBorder(BorderFactory.createEmptyBorder(0,0,0,50));
                center.add(rightEmpty,BorderLayout.EAST);

                cancelBtn = new JButton("重置");
                confirmBtn = new JButton("确定");
                centerBottom.add(confirmBtn);
                centerBottom.add(cancelBtn);
                center.add(centerBottom,BorderLayout.SOUTH);

                bottom = new JPanel(new GridLayout(2,1));
//                JLabel register = new JLabel("还没注册账号?");
                JLabel register = new LinkLabel("还没注册账号么?", Constants.ChatHost +"/register");
                JLabel forget = new JLabel("忘记密码?");
                register.setBorder(BorderFactory.createEmptyBorder(0,0,10,20));
                forget.setBorder(BorderFactory.createEmptyBorder(0,20,10,0));
                JPanel last = new JPanel();
                last.add(register);
                last.add(forget);

                this.add(top,BorderLayout.NORTH);
                this.add(center,BorderLayout.CENTER);

                JPanel errorPanel = new JPanel();
                error = new JLabel();
                error.setForeground(Color.red);
                errorPanel.add(error);

                bottom.add(errorPanel,BorderLayout.NORTH);
                bottom.add(last,BorderLayout.SOUTH);

                this.add(bottom,BorderLayout.SOUTH);

                this.setLocationRelativeTo(null);

                this.setVisible(true);

                setOperator();
                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }

        private void setOperator(){
              confirmBtn.addActionListener(new ActionListener() {
                      @Override
                      public void actionPerformed(ActionEvent e) {
                            login(account.getText(),new String(passwordField.getPassword()));
                      }
              });


              cancelBtn.addActionListener(new ActionListener() {
                      @Override
                      public void actionPerformed(ActionEvent e) {
                             account.setText("");
                             passwordField.setText("");
                      }
              });
        }

        /**
         * 登录
         * @param username
         * @param password
         */
        public void login(String username,String password){

                UserServicce userService = new UserServicce();
                UserEntity userEntity = userService.selectUserByUsername(username);
                if(userEntity != null){
                     if(userEntity.getPassword().equals(password)){
                          ManagerWind.user= JSONObject.parseObject(JSONObject.toJSONString(userEntity),User.class);

                          ArrayList<UserEntity> list  = userService.selectAll();  // 获取好友
                          String str = JSONArray.toJSONString(list);
                          ManagerWind.friends = JSONArray.parseArray(str, User.class);
                          ManagerWind managerWind = new ManagerWind();
                          managerWind.connect();
                          this.setVisible(false);
                          return;
                     }
                     error.setText("密码不正确!");
                }else{
                    error.setText("该用户不存在，请注册!");
                }

        }
}
